package com.lanou.moving.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.SingerSimilar;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/24.
 */
public class SingerInfoAdapter extends BaseAdapter {
    private Context context;
    private List<SingerSimilar.DataEntity> singers;
    private SingerSimilar.DataEntity singer;

    public SingerInfoAdapter(Context context, List<SingerSimilar.DataEntity> singers) {
        this.context = context;
        this.singers = singers;
    }

    @Override
    public int getCount() {
        return singers != null && singers.size() > 0 ? singers.size() : 0;
    }

    @Override
    public SingerSimilar.DataEntity getItem(int position) {
        return singers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.singer_info_gridview_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        singer = getItem(position);

        if (singer != null) {
            Picasso.with(context).load(singer.getPicUrl()).placeholder(R.mipmap.singer_similar)
                    .error(R.mipmap.singer_similar).into(holder.singerPicture);
            holder.singerName.setText(singer.getName());
        }

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'singer_info_gridview_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.singer_picture)
        CircleImageView singerPicture;
        @Bind(R.id.singer_name)
        TextView singerName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
